﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Compression;
using Ionic.Zip;
using System.IO;

namespace ZipArchiver
{
    public class ZipArchiver
    {
        public static void CreateArchive(string filename)
        {
            using (FileStream fs = new FileStream(filename, FileMode.Open))
            {
                Stream outStream = new FileStream(filename+".zip", FileMode.Create);
                using (ZipOutputStream zos = new ZipOutputStream(outStream))
                {
                    zos.PutNextEntry(filename);
                    byte[] buff = new byte[4096];
                    while (true)
                    {
                        int bytesRead = fs.Read(buff, 0, buff.Length);
                        if (bytesRead == 0)
                        {
                            break;
                        }

                        zos.Write(buff, 0, bytesRead);
                    }
                }
            }
        }

        public static void ExtractArchive(string archive)
        {
            using (ZipInputStream zis = new ZipInputStream(archive))
            {
                ZipEntry archivedFile = zis.GetNextEntry();
                using (FileStream fs = new FileStream("extracted_" + archivedFile.FileName, FileMode.Create))
                {
                    byte[] buff = new byte[4096];
                    while (true)
                    {
                        int bytesRead = zis.Read(buff, 0, buff.Length);
                        if (bytesRead == 0)
                        {
                            break;
                        }

                        fs.Write(buff, 0, bytesRead);
                    }
                }
            }
        }

        public static void Main()
        {
            CreateArchive("test.txt");
            ExtractArchive("test.txt.zip");
        }
    }
}
